package com.unohacha.qqj.mylibrary.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.unohacha.qqj.mylibrary.AppManager;
import com.unohacha.qqj.mylibrary.R;
import com.unohacha.qqj.mylibrary.listener.LifeCycleListener;
import com.unohacha.qqj.mylibrary.listener.PermissionListener;
import com.unohacha.qqj.mylibrary.util.StatusBarUtil;
import com.unohacha.qqj.mylibrary.util.T;
import com.unohacha.qqj.mylibrary.view.CommentPopWindow;
import com.unohacha.qqj.mylibrary.view.ConfirmPopWindow;
import com.unohacha.qqj.mylibrary.view.LoadingDialog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @description: BaseActivity
 * @data 2018/1/24-16:09
 * @author: AoJiaoQiang
 */
public abstract class BaseActivity extends RxAppCompatActivity {

    private Toast toast;
    private Bundle mBundle;
    private Unbinder unbinde;
    private QMUITipDialog mDialog;
    private LoadingDialog mLoadingDialog;
    private PermissionListener mPermisListener;
    public ConfirmPopWindow confirmPop;
    public CommentPopWindow commentPopWindow;
    private static final int PERMISSION_REQUESTCODE = 100;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        unbinde = ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        mBundle = getIntent().getExtras();
        initBundle(mBundle);
        setStatusBarColor();
        T.init(this);
        confirmPop = new ConfirmPopWindow(BaseActivity.this, BaseActivity.this);
        commentPopWindow = new CommentPopWindow(BaseActivity.this,BaseActivity.this);
        initData();
        initView();
            
    }
    

    protected AppManager getAppManager() {
        return AppManager.getAppManager();
    }

    protected abstract int getLayoutId();

    protected void initBundle(Bundle bundle) {
    }

    protected void initView() {
    }

    protected void initData() {
    }

    protected QMUITipDialog getLoading() {
        if (mDialog == null) {
            mDialog = new QMUITipDialog.Builder(this)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(getResources().getString(R.string.loading))
                    .create();
        }
        return mDialog;
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    public void requestRunPermisssion(String[] permissions, PermissionListener listener) {
        mPermisListener = listener;
        List<String> permissionLists = new ArrayList<>();
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                permissionLists.add(permission);
            }
        }

        if (!permissionLists.isEmpty()) {
            requestPermissions(permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUESTCODE);
        } else {
            //表示全都授权了
            mPermisListener.onGranted();
        }
    }

    protected <T extends Serializable> T getBundleSerializable(String key) {
        if (mBundle == null) {
            return null;
        }
        return (T) mBundle.getSerializable(key);
    }

    protected void finishActivity(Class<? extends Activity> cls) {
        AppManager.getAppManager().finishActivity(cls);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mListener != null) {
            mListener.onStart();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mListener != null) {
            mListener.onRestart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mListener != null) {
            mListener.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mListener != null) {
            mListener.onPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mListener != null) {
            mListener.onStop();
        }
    }


    protected int getStatusBarColor() {
        return getResources().getColor(R.color.colorPrimary);
    }

    /**
     * 设置状态栏的颜色
     *
     * @param color
     */
    public void setStatusBarColor(int color) {
        StatusBarCompat.setStatusBarColor(this, color);
        System.out.println("---------" + Build.VERSION.SDK_INT);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    /**
     * 生命周期回调函数
     */
    public LifeCycleListener mListener;

    public void setOnLifeCycleListener(LifeCycleListener listener) {
        mListener = listener;
    }

    /**
     * 设置状态栏颜色
     */
    private void setStatusBarColor() {
        StatusBarUtil.immersive(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//此段代码activity头会被顶上去
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    public void jumpToOtherActivity(Context context, Class clazz) {
        Intent intent = new Intent(context, clazz);
        startActivity(intent);
    }

    public void MyToast(String s) {
        if (toast == null)
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        else
            toast.setText(s);
        toast.show();
    }

    public void MyToast(int s) {
        if (toast == null)
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        else
            toast.setText(s);
        toast.show();
    }

    public void showLoading() {
        if (null != mLoadingDialog) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
        }

        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setMessage("加载中...");
            mLoadingDialog.setCanceledOnTouchOutside(false);
            mLoadingDialog.show();
        } else if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void showLoading(String msg) {

        if (null != mLoadingDialog) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
        }

        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setMessage(msg);
            mLoadingDialog.setCanceledOnTouchOutside(false);
            mLoadingDialog.show();
        } else if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    public void closrKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mListener != null) {
            mListener.onDestroy();
        }
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
        // 移除view绑定
        if (unbinde != null) {
            unbinde.unbind();
        }
        AppManager.getAppManager().finishActivity(this);
    }
}
